﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;
using System.Data;

public partial class Admin_TheHoiVien_QLMuaThe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindLoaiThe(cboLoaiThe);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        DataSet dsMaThe = DataRepository.Provider.ExecuteDataSet("MuaThe_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsMaThe.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsMaThe.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLTheHoiVien.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            if (chkSuDung.Checked == true)
            {
                TheHoiVien entTheHoiVien = new TheHoiVien();
                entTheHoiVien.MaThe = Common.StandardString(txtMaThe.Text);
                int TotalRows = 0;
                DataRepository.MaTheHvProvider.GetPaged("MaThe = " + txtMaThe.Text, "Id ASC", 0, 0, out TotalRows);
                if (TotalRows > 0)
                    lblMaThe.Text = "Mã thẻ so sánh khớp";
                else
                    return;

                entTheHoiVien.HoTen = Common.StandardString(txtHoTen.Text);
                entTheHoiVien.DiaChi = Common.StandardString(txtAddress.Text);
                entTheHoiVien.Phone = Common.StandardString(txtPhone.Text);
                entTheHoiVien.Email = Common.StandardString(txtEmail.Text);
                entTheHoiVien.IdLoaiThe = Convert.ToInt32(cboLoaiThe.SelectedValue);
                entTheHoiVien.SoCmnd = tbSocmnd.Text;

                // Them ngay dang ky chinh sua mua the


                DateTime myDate = DateTime.Now;
                DateTime dt1 = DateTime.ParseExact(Common.StandardString(txtNgaySinh.Text), "dd/MM/yyyy", null);
                //TimeSpan dt2 = myDate.TimeOfDay;
                //TimeSpan dt3 = dt1.TimeOfDay;
                if (dt1 >= myDate)
                {
                    lblNgaySinh.Text = "Ngày sinh không thể lớn hơn ngày hiện tại";
                    return;
                }

                else
                    entTheHoiVien.NgaySinh = DateTime.ParseExact(Common.StandardString(txtNgaySinh.Text), "dd/MM/yyyy", null);

                entTheHoiVien.NgayCapNhat = Common.GetServerDateTime();
                entTheHoiVien.NgayDangKy = DateTime.ParseExact(tbNgayDangky.Text.ToString(), "dd/MM/yyyy", null);
                entTheHoiVien.TrangThai = chkSuDung.Checked;

                updateStatus = DataRepository.TheHoiVienProvider.Insert(entTheHoiVien);

                if (updateStatus)
                {
                    DataRepository.MuaTheProvider.Delete(Convert.ToInt32(H_CatalogId.Value));
                    Pager1.CurrentIndex = 1;
                    Bind_Data(Pager1.CurrentIndex);

                    cmdReset_Click(sender, e);
                }
                else
                    WebMessage.Show("Cập nhật Thẻ Hội Viên thất bại!");
            }

            else
            {
                WebMessage.Show("Chưa xác nhận cho khách hàng");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLMuaThe.aspx.cs", "cmdSave_Click", ex.ToString());
            WebMessage.Show("Cập nhật Thẻ Hội Viên thất bại!");
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtMaThe.Text = "";
        chkSuDung.Checked = true;
        H_CatalogId.Value = "0";

        txtHoTen.Text = "";
        lblMaThe.Text = "";
        lblNgaySinh.Text = "";
        txtAddress.Text = "";
        txtEmail.Text = "";
        txtNgaySinh.Text = "";
        txtPhone.Text = "";

        cboLoaiThe.SelectedValue = "0";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                MuaThe entTheHoiVien = DataRepository.MuaTheProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entTheHoiVien != null)
                {
                    chkSuDung.Checked = entTheHoiVien.TrangThai;
                    txtMaThe.Text = entTheHoiVien.Id.ToString();
                    txtHoTen.Text = entTheHoiVien.HoTen;
                    txtAddress.Text = entTheHoiVien.DiaChi;
                    txtEmail.Text = entTheHoiVien.Email;
                    txtNgaySinh.Text = entTheHoiVien.NgaySinh.ToString("dd/MM/yyyy");
                    txtPhone.Text = entTheHoiVien.Phone;
                    tbNgayDangky.Text = entTheHoiVien.NgayCapNhat.ToShortDateString();
                    tbSocmnd.Text = entTheHoiVien.SoCmnd.ToString(); // So CMND
                    cboLoaiThe.SelectedValue = entTheHoiVien.IdLoaiThe.ToString();
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLMuaThe.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdTheHV = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.MuaTheProvider.Delete(IdTheHV);
                if (!deleteStatus)
                    WebMessage.Show("Xóa Thẻ Hội Viên thất bại");
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLMuaThe.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                WebMessage.Show("Xóa Thẻ Hội Viên thất bại");
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Pager1.CurrentIndex = 1;
        Bind_Data(Pager1.CurrentIndex);
        txtMaThe.Text = "";
        chkSuDung.Checked = false;
        H_CatalogId.Value = "0";

        txtHoTen.Text = "";
        lblMaThe.Text = "";
        lblNgaySinh.Text = "";
        txtAddress.Text = "";
        txtEmail.Text = "";
        txtNgaySinh.Text = "";
        txtPhone.Text = "";

        cboLoaiThe.SelectedValue = "0";
    }

    protected void MaThe_Changed(object sender, EventArgs e)
    {
        int TotalRows = 0;
        DataRepository.MaTheHvProvider.GetPaged("MaThe = " + txtMaThe.Text, "Id ASC", 0, 0, out TotalRows);
        if (TotalRows > 0)
            lblMaThe.Text = "Mã thẻ so sánh khớp";
        else
            lblMaThe.Text = "Mã thẻ so sánh không khớp";
    }

    public string GetLoaiThe(object Char)
    {
        int idLoaiThe = Convert.ToInt32(Char.ToString());
        using (LoaiThe entLoaiThe = DataRepository.LoaiTheProvider.GetById(idLoaiThe))
        {
            return entLoaiThe.TenLoaiThe;
        }
    }
}